package com.pmy.controller;

import com.pmy.pojo.Bill;
import com.pmy.pojo.Provider;
import com.pmy.service.BillServiceImpl;
import com.pmy.service.ProviderServiceImpl;
import com.pmy.util.PageSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class BillController {
    @Autowired
    BillServiceImpl billService;
    @Autowired
    ProviderServiceImpl providerService;

    @RequestMapping(value = "/billList")
    public String billList(Model model, HttpServletRequest request){

        String proName = request.getParameter("queryProName");
        String temp = request.getParameter("queryIsPayment");
        String productName = request.getParameter("queryProductName");

        String pageIndex = request.getParameter("pageIndex");//当前页
        //设置页面容量
        int pageSize = 10;//写死页面容量
        //当前页
        int currentPageNo = 1;//第一次当前页写死为第一页
        if (proName == ""){
            proName = null;//如果为空字符串设置为Null
        }
        if (productName == ""){
            productName = null;
        }
        if(temp == ""){
            temp = null;
        }

        String isPayment = temp;

        if (pageIndex != null){
            try {
                currentPageNo = Integer.parseInt(pageIndex);
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        }
        System.out.println("***********************************************");
        System.out.println(proName);
        System.out.println(productName);
        System.out.println(isPayment);
        System.out.println("***********************************************");

        int totalCount = 0;//用户数据条数
        List<String> providerName = null;
        try {
            //获取用户列表
            providerName = providerService.getProviderName();
            //获取用户总数
            totalCount = billService.getBillCount(productName, proName, isPayment);
            //总页数支持
        } catch (Exception e) {
            e.printStackTrace();
        }

        PageSupport pageSupport = new PageSupport();
        pageSupport.setCurrentPageNo(currentPageNo);
        pageSupport.setPageSize(pageSize);
        pageSupport.setTotalCount(totalCount);

        //控制首页和尾页、如果页面小于1了，就显示第一页
        int totalPageCount = pageSupport.getTotalPageCount();
        if (totalPageCount < 1){
            currentPageNo = 1;
        }else if(currentPageNo > totalPageCount){
            currentPageNo = totalPageCount;
        }


        //获取用户数据
        List<Bill> bills = null;
        try {
            bills = billService.queryAll(productName, proName,isPayment, (currentPageNo - 1) * pageSize, pageSupport.getPageSize());
        } catch (Exception e) {
            e.printStackTrace();
        }

        //前端需要的值
        model.addAttribute("queryProductName",productName);
        model.addAttribute("queryProName",proName);
        model.addAttribute("isPayment",isPayment);
//        订单信息
        model.addAttribute("bills",bills);
//        供应商列表
        model.addAttribute("providerName",providerName);


        //分页支持
        model.addAttribute("totalPageCount",totalPageCount);
        model.addAttribute("totalCount",totalCount);
        model.addAttribute("currentPageNo",currentPageNo);
        model.addAttribute("totalPageCount",totalPageCount);

        return "billlist";
    }
//    增加一条订单
        @RequestMapping(value = "/reachBill")
        public String reachUser(String billId,Model model){
        //        为空返回添加页面
            if (Integer.parseInt(billId) == 0){
                List<String> providerName = providerService.getProviderName();
                model.addAttribute("providerName",providerName);
                return "addbill";
            }
        //          userId有值返回到修改页面
            Bill bill = null;
            List<String> providerName = null;
            Provider provider = null;
            try {
                bill = billService.getBillById(Integer.parseInt(billId));
                providerName = providerService.getProviderName();
                int providerId = Math.toIntExact(bill.getProviderId());
                provider = providerService.getProviderById(providerId);

            } catch (NumberFormatException e) {
                e.printStackTrace();
            }

            model.addAttribute("providerName",providerName);
            model.addAttribute("bill",bill);
            model.addAttribute("provide",provider);
            return "updatebill";
        }
        @RequestMapping(value = "/addbill")
        public String addBill(Bill bill,HttpServletRequest request){

            String proName = request.getParameter("proNameTest");
            int bi = 0;
            try {
                List<Provider> providers = providerService.queryAll(null, proName, 0, 20);
                for (Provider provider : providers) {
                    bill.setProviderId((long) provider.getId());
                }
                bi = billService.addBill(bill);
            } catch (Exception e) {
                e.printStackTrace();
            }


            if (bi == 1){
                request.getSession().setAttribute("msg1","添加成功");
            }else {
                request.getSession().setAttribute("msg0","添加失败");
            }


            return "redirect:/billList";
        }

        @RequestMapping(value = "updateBill")
        public String updateBill(Bill bill,HttpServletRequest request){
            String proName = request.getParameter("proNameTest");
            int bi = 0;
            try {
                List<Provider> providers = providerService.queryAll(null, proName, 0, 20);
                for (Provider provider : providers) {
                    bill.setProviderId((long) provider.getId());
                }
                bi = billService.updateBillById(bill);
            } catch (Exception e) {
                e.printStackTrace();
            }

            if (bi == 1){
                request.getSession().setAttribute("msg1","修改成功");
            }else {
                request.getSession().setAttribute("msg0","修改失败");
            }


            return "redirect:/billList";
        }
//        删除的实现
        @RequestMapping(value = "deleteBill")
        public String deleteBill(int id,HttpServletRequest request){

            int bi = 0;
            try {
                bi = billService.deleteBillById(id);
            } catch (Exception e) {
                e.printStackTrace();
            }

            if (bi == 1){
                request.getSession().setAttribute("msg1","删除成功");
            }else {
                request.getSession().setAttribute("msg0","删除失败");
            }

            return "redirect:/billList";
        }
}
